const userDb = require('../db/user-db')

const checkToken = function (token,maddr) {
    return new Promise((resolve, reject) => {
        let messageBody = {};
        userDb.tokenExist(maddr)
            .then((result) => {
                if(!result){
                    messageBody.code = 10007;
                    messageBody.body = 'user does not exist';
                    resolve(messageBody);
                }else{
                    if(token!==result.token){
                        messageBody.code = 10010;
                        messageBody.body = 'Token has expired';
                        resolve(messageBody);
                    }else{
                        messageBody.code = 0;
                        messageBody.body = result;
                        resolve(messageBody);
                    }
                }
            })
            .catch((err) => {
                messageBody.code = 10001;
                messageBody.body = err;
                resolve(messageBody);
            })
    });


};

module.exports = checkToken;
